package com.sx.manage.service.company;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.sx.common.util.BeanUtils;
import com.sx.manage.biz.company.IMCompanyBiz;
import com.sx.manage.exception.MAjaxErrorRollbackException;
import com.sx.manage.service.consumer.IMBaseCompanyConsumerInfoService;
import com.sx.manage.service.consumer.IMCompanyConsumerInfoService;
import com.sx.manage.service.consumer.company.temp.IMCompanyInfoTempService;
import com.sx.model.constants.MDataStatusType;
import com.sx.model.constants.type.company.MAuditingStatusType;
import com.sx.model.manage.mybatis.form.company.MCompanyInfoTempForm;
import com.sx.model.manage.mybatis.form.consumer.MCompanyConsumerInfoForm;
import com.sx.model.manage.mybatis.form.consumer.MCompanyForm;
import com.sx.model.manage.mybatis.vo.company.MCompanyInfoTempVO;
import com.sx.model.manage.mybatis.vo.consumer.company.MComConsumerVO;
import com.sx.model.manage.mybatis.vo.consumer.login.MConsumerLoginVO;
import com.sx.model.manage.mybatis.vo.result.ResultBooleanVO;
import com.sx.model.manage.mybatis.vo.result.ResultListVO;
import com.sx.model.manage.mybatis.vo.result.ResultObjectVO;

/**
 * 公司信息临时 webservice
 * 
 * @author Administrator
 *
 */
@Service("companyInfoTempWebServiceImpl")
@Transactional(propagation=Propagation.REQUIRED)
public class MCompanyInfoTempWebServiceImpl {
	/**
	 * 日志信息
	 */
	private Logger _log = LoggerFactory.getLogger(this.getClass());
	// 公司信息临时
	@Resource(name = "companyInfoTempServiceImpl")
	private IMCompanyInfoTempService companyinfotempserviceimpl;


	//公司正式信息
	@Resource(name="mBaseCompanyServiceImpl")
	private IMBaseCompanyService mBaseCompanyServiceImpl;
	
	@Resource(name="mCompanyBizImpl")
	private IMCompanyBiz mCompanyBizImpl;
	//公司用户
	@Resource(name="mCompanyConsumerInfoServiceImpl")
	private IMCompanyConsumerInfoService mCompanyConsumerInfoServiceImpl;

	//公司用户
	@Resource(name="mBaseCompanyConsumerInfoServiceImpl")
	private IMBaseCompanyConsumerInfoService mBaseCompanyConsumerInfoServiceImpl;
	
	public IMCompanyInfoTempService getCompanyinfotempserviceimpl() {
		return companyinfotempserviceimpl;
	}

	public void setCompanyinfotempserviceimpl(
			IMCompanyInfoTempService companyinfotempserviceimpl) {
		this.companyinfotempserviceimpl = companyinfotempserviceimpl;
	}

	/**编辑数据*/
	public ResultBooleanVO update(MCompanyInfoTempForm form,MConsumerLoginVO loginConsumer){
		return companyinfotempserviceimpl.update(form, loginConsumer);
	}

	/**公司信息验证通过*/
	public ResultBooleanVO pass(Long id, MConsumerLoginVO loginConsumer){
		_log.debug("公司临时信息验证通过 id="+id);
		if( null == id){
			_log.error("参数为空");
			return new ResultBooleanVO("m.parameter.is.null");
		}
		ResultObjectVO<MCompanyInfoTempVO> objResult = companyinfotempserviceimpl.queryById(id);
		
		if( null ==  objResult.getObjVo()){
			_log.error("数据不存在");
			return new ResultBooleanVO(false,null,"数据不存在");
		}
		if( MAuditingStatusType.PASS.equals( objResult.getObjVo().getImport_status() )){
			_log.error("已经通过的数据，不能在进行通过操作");
			return new ResultBooleanVO(false,null,"已经通过的数据，不能在进行通过操作");
		}
			_log.debug("修改公司状态为通过");		
			ResultBooleanVO result = companyinfotempserviceimpl.editStatus( MAuditingStatusType.PASS, id, loginConsumer);
			if(!result.isSuccess()){
				_log.debug("数据进入正式库时错误："+result.getMsg());
				return result;
			}
			MCompanyForm form = this.createCompanyForm( objResult.getObjVo());
			_log.debug("公司数据进入正式库");		
									     result = mCompanyBizImpl.company_save(form,loginConsumer);
			if(!result.isSuccess()){
				_log.debug("数据进入正式库时错误："+result.getMsg());
				throw new MAjaxErrorRollbackException( result.getMsg() );//返回到配置文件中定义的路径
			}	
			
			_log.debug("查询公司下的用户");
			MCompanyConsumerInfoForm companyConsumerform = new MCompanyConsumerInfoForm();
								     companyConsumerform.setCompany_id( id);
								     
			ResultListVO<MComConsumerVO> comConsumerResultVO = mBaseCompanyConsumerInfoServiceImpl.get_consumerList(companyConsumerform);
			if( null != comConsumerResultVO.getData() && !comConsumerResultVO.getData().isEmpty()){
				Long[] userIds = new Long[comConsumerResultVO.getData().size()];
				for(int i=0,l=comConsumerResultVO.getData().size();i<l;i++){
					userIds[i]=comConsumerResultVO.getData().get(i).getId();
				}
				_log.debug("修改用户信息状态");
				ResultBooleanVO editStatusResult  = mCompanyConsumerInfoServiceImpl.consumersEnable(userIds);
				if(!editStatusResult.isSuccess()){
					_log.error("修改出错："+editStatusResult.getMsg());
					throw new MAjaxErrorRollbackException( editStatusResult.getMsg() );//返回到配置文件中定义的路径
				}
			}
			
			return new ResultBooleanVO() ;
	}
	/** 临时信息转正式信息**/
	private MCompanyForm createCompanyForm(MCompanyInfoTempVO tempVO){
		MCompanyForm companyForm = new MCompanyForm();
		try{
			BeanUtils.copyProperties(tempVO, companyForm, false);
			 //
			 companyForm.setStatus( MDataStatusType.DATA_AVAILABLE.toString() );
			 companyForm.setCompany_create_time( tempVO.getCreate_time());			 
			 companyForm.setCompany_code(this.mBaseCompanyServiceImpl.getCompanyCode() );
			 companyForm.setChannel_id(tempVO.getChannel_id());
		}catch(Exception e){
			_log.error("公司临时信息vo转换正式信息from时出错：",e);
		}		
		return companyForm;
	}
	/**根据id查询用户*/
	public ResultObjectVO<MCompanyInfoTempVO> queryById(Long id){
		return companyinfotempserviceimpl.queryById(id);
	}

	/**查询分页*/
	public ResultListVO<MCompanyInfoTempVO> queryPage(MCompanyInfoTempForm form){
		return companyinfotempserviceimpl.queryPage(form);
	}
	
}
